/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.ticket.servicefu.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.ticket.servicefu.entity.ServiceFu;
import com.jeeplus.modules.ticket.servicefu.service.ServiceFuService;
import redis.clients.jedis.Jedis;

/**
 * 教辅服务对象Controller
 * @author 刘明星
 * @version 2021-10-04
 */
@RestController
@RequestMapping(value = "/ticket/servicefu/serviceFu")
public class ServiceFuController extends BaseController {

	@Autowired
	private ServiceFuService serviceFuService;

	@ModelAttribute
	public ServiceFu get(@RequestParam(required=false) String id) {
		ServiceFu entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = serviceFuService.get(id);
		}
		if (entity == null){
			entity = new ServiceFu();
		}
		return entity;
	}

	/**
	 * 教辅服务对象列表数据
	 */
//	@RequiresPermissions("ticket:servicefu:serviceFu:list")
	@GetMapping("list")
	public AjaxJson list(ServiceFu serviceFu, HttpServletRequest request, HttpServletResponse response) {
		Jedis jedis = new Jedis("localhost");
		String admin = request.getParameter("admin");
		if(!admin.equals("admin")){
			String key = request.getParameter("key");
			if(jedis.exists(key)){
				return AjaxJson.success("redis").put("value",jedis.get(key));
			}
		}
		Page<ServiceFu> page = serviceFuService.findPage(new Page<ServiceFu>(request, response), serviceFu);
		return AjaxJson.success().put("page",page);

	}

	/**
	 * 根据Id获取教辅服务对象数据
	 */
//	@RequiresPermissions(value={"ticket:servicefu:serviceFu:view","ticket:servicefu:serviceFu:add","ticket:servicefu:serviceFu:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(ServiceFu serviceFu) {
		return AjaxJson.success().put("serviceFu", serviceFu);
	}

	/**
	 * 保存教辅服务对象
	 */
//	@RequiresPermissions(value={"ticket:servicefu:serviceFu:add","ticket:servicefu:serviceFu:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.save(serviceFu);//保存
		return AjaxJson.success("保存教辅服务对象成功");
	}

	@PostMapping("updateGood")
	public AjaxJson updateGood(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateGood(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updateFine")
	public AjaxJson updateFine(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateFine(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updateGeneral")
	public AjaxJson updateGeneral(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateGeneral(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updatePoor")
	public AjaxJson updatePoor(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updatePoor(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updateSpeakGood")
	public AjaxJson updateSpeakGood(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateSpeakGood(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updateSpeakFine")
	public AjaxJson updateSpeakFine(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateSpeakFine(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updateSpeakGeneral")
	public AjaxJson updateSpeakGeneral(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateSpeakGeneral(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	@PostMapping("updateSpeakPoor")
	public AjaxJson updateSpeakPoor(ServiceFu serviceFu, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(serviceFu);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		serviceFuService.updateSpeakPoor(serviceFu);//保存
		return AjaxJson.success("保存数据成功");
	}

	/**
	 * 批量删除教辅服务对象
	 */
//	@RequiresPermissions("ticket:servicefu:serviceFu:del")
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			serviceFuService.delete(new ServiceFu(id));
		}
		return AjaxJson.success("删除教辅服务对象成功");
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("ticket:servicefu:serviceFu:export")
    @GetMapping("export")
    public AjaxJson exportFile(ServiceFu serviceFu, HttpServletRequest request, HttpServletResponse response) {
		try {
            String fileName = "教辅服务对象"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<ServiceFu> page = serviceFuService.findPage(new Page<ServiceFu>(request, response, -1), serviceFu);
    		new ExportExcel("教辅服务对象", ServiceFu.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error("导出教辅服务对象记录失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("ticket:servicefu:serviceFu:import")
    @PostMapping("import")
   	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<ServiceFu> list = ei.getDataList(ServiceFu.class);
			for (ServiceFu serviceFu : list){
				try{
					serviceFuService.save(serviceFu);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条教辅服务对象记录。");
			}
			return AjaxJson.success( "已成功导入 "+successNum+" 条教辅服务对象记录"+failureMsg);
		} catch (Exception e) {
			return AjaxJson.error("导入教辅服务对象失败！失败信息："+e.getMessage());
		}
    }

	/**
	 * 下载导入教辅服务对象数据模板
	 */
	@RequiresPermissions("ticket:servicefu:serviceFu:import")
    @GetMapping("import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		try {
            String fileName = "教辅服务对象数据导入模板.xlsx";
    		List<ServiceFu> list = Lists.newArrayList();
    		new ExportExcel("教辅服务对象数据", ServiceFu.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			return AjaxJson.error( "导入模板下载失败！失败信息："+e.getMessage());
		}
    }


}